﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using System.Runtime.Serialization;


namespace Chat
{
    [Serializable] 
    class Message //: ISerializable
    {

        public Message()
        {
        }

        public byte[] Serialize()
        {
            /*

                1) Create a new MemoryStream class with the CanWrite property set to true
                (should be by default, using the default constructor).

                2) Create a new instance of the BinaryFormatter class.

                3) Pass the MemoryStream instance and your object to be serialized to the
                Serialize method of the BinaryFormatter class.

                4) Call the ToArray method on the MemoryStream class to get a byte array
                with the serialized data.

            */


            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf1 = new BinaryFormatter();
            bf1.Serialize(ms, this);
            return ms.ToArray();
        }


        public static Message Deserialize(byte[] theByteArray, int len)
        {
            //MemoryStream ms = new MemoryStream(theByteArray,0,len);
            MemoryStream ms = new MemoryStream(theByteArray);
            //MemoryStream ms = new MemoryStream();
            //ms.Write(theByteArray, 0, len);
            BinaryFormatter bf1 = new BinaryFormatter();
            
            //ms.Position = 0;
            //ms.Seek(0, 0);
            Message temp = (Message)bf1.Deserialize(ms);
            return temp;
        }
   }
}
